<?php
require_once "../../php/config/config.php";
require_once "../model/adminDb.php";
$AdminDb = new AdminDb();
$name = $_SESSION['name'];
$data = $AdminDb->query_userName($name);
$card = $data[0]['card'];


?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="../../static/js/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/css/view.css"/>
    <title></title>
    <style>
        #test10 {
            margin-left: 100px;
        }

        .imgCss {
            margin-left: 20px;
            height: 200px;
            cursor: pointer;
        }

    </style>
</head>
<body class="">
<form class="layui-form layui-card-body" lay-filter="example" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名/账号：</label>
        <div class="layui-input-inline">
            <input name="name" class="layui-input" type="text" placeholder="请输入姓名或账号" autocomplete="off"
                   lay-verify="name">
        </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码：</label>
        <div class="layui-input-inline">
            <input name="password" id="password" class="layui-input" type="password" placeholder="请输入密码"
                   autocomplete="off"
                   lay-verify="pass">
        </div>
        <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
    </div>

    <?php
    if ($card == 1) {
        echo " <div class=\"layui-form-item\">
        <label class=\"layui-form-label\">角色：</label>
        <div class=\"layui-input-block\">
            <select name=\"card\" id=\"card\" lay-filter=\"card\" >
                <option value=\"\"></option>
                <option value=\"1\">管理员</option>
                <option value=\"2\">普通用户</option>
            </select>
        </div>
    </div>";
    } else {
        echo " <div class=\"layui-form-item\">
        <label class=\"layui-form-label\">角色：</label>
        <div class=\"layui-input-block\">
            <select name=\"card\" id=\"card\" disabled lay-filter=\"card\" >
                <option value=\"\"></option>
                <option value=\"1\">管理员</option>
                <option value=\"2\">普通用户</option>
            </select>
        </div>
    </div>";
    }


    ?>


    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">理由：</label>
        <div class="layui-input-block">
            <textarea name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <br>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <a class="layui-btn layui-btn-blue" lay-submit lay-filter="addUser">立即修改</a>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    <input type="text" name="userCard" id="userCard" disabled value="<?php echo $card ?>"
           style="display: none">
</form>
<script type="text/javascript" src="../../static/js/jquery-3.3.1.min.js"></script>
<script src="../../static/js/layui/layui.all.js"></script>

<script>
    var url = document.location.toString();
    var id = url.split("=")[1];

    query(id);

    //获取user的信息
    function query(id) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/query_user_id.php',
            data: {
                "id": id,
            },
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {//获取成功
                    assignment(res.data);
                }
            },
            error: function (res) {
                layer.msg("连接失败！");
            }
        });
    }

    //给表单赋值
    function assignment(value) {
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            //数据提取
            $name = value[0].name;
            $card = value[0].card;
            $desc = value[0].desction;


            //表单初始赋值
            form.val('example', {
                "name": $name,
                "card": $card,
                "desc": $desc,
            });


        });
    }

    //验证密码
    function verifyPass(data) {

        //prompt层
        layer.prompt({title: '请输入旧密码，并确认', formType: 1}, function (pass, index) {
            if (pass.length == 0) {
                layer.msg("密码还没有填");
            }
            else {
                $.ajax({
                    type: 'post',
                    cache: 'false',
                    url: '../controller/verifryPass.php',
                    data: {
                        "pass": pass,
                    },
                    dataType: 'json',
                    success: function (res) {
                        layer.msg(res.msg);
                        if (res.code == 200) {
                            layer.close(index);
                            newPass(data);

                        }

                    },
                    error: function (dat) {
                        layer.msg("验证失败");

                    }


                });
            }


        });
    }

    //验证输入的新密码
    function newPass(data) {
        layer.prompt({title: '确认新密码，并确认', formType: 1}, function (pass, index) {
            $newPass = $("#password").val();
            if (pass.length >= 6) {
             if($newPass==pass){

                 updateUser(data);
                 layer.close(index);

             }
             else {
                 layer.msg("操作失败，两次密码不一致");
             }



            }
            else {
                layer.msg("密码长度必须大于等于六位");
            }

        });
    }

    //添加user用户
    layui.use('form', function () {
        var form = layui.form;
        //自定义验证规则
        form.verify({
            name: function (value) {
                if (value.length < 1) {
                    return '账号或姓名太短';
                }
            },
            pass: function (value) {
                if (value.length < 5) {
                    return '密码至少大于六位';
                }
            },
        });


        //监听提交
        form.on('submit(addUser)', function (data) {

            $card = $("#card").val();

            if ($card == "") {
                layer.msg("未选择角色");
                return false;
            }
            var data = data.field;
            console.log(data);
            $userCard = $("#userCard").val();
            if ($userCard == 1) {
                updateUser(data);
            }
            else {
                verifyPass(data);
            }


            return false;


        });
    });

    //添加
    function updateUser(data) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/update_user.php',
            data: {
                "id":id,
                "data": data,
            },
            dataType: 'json',
            success: function (res) {
                layer.msg(res.msg);
                if(res.code==200){
                    $card = $("#card").val();
                    if($card==1){
                        close();
                    }
                    else {
                        close();
                    }


                }
                return false;
            },
            error: function (dat) {
                layer.msg("添加失败");
                return false;
            }


        });
    }

</script>
</body>
</html>